package com.tong.embeddedvr.service.impl;

import com.tong.embeddedvr.Mapper.CollectionMapper;
import com.tong.embeddedvr.dto.CollectionDTO;
import com.tong.embeddedvr.dto.TableReqDTO;
import com.tong.embeddedvr.dto.TableRspDTO;
import com.tong.embeddedvr.dto.UserInputDTO;
import com.tong.embeddedvr.entity.CollectionEntity;
import com.tong.embeddedvr.entity.UserEntity;
import com.tong.embeddedvr.service.CollectionService;
import com.tong.embeddedvr.utils.CommonUtil;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * TODO
 *
 * @author gcl
 * @version 1.0
 * @Description
 * @date 2020/12/28 19:51
 */
@Service
public class CollectionServiceImpl implements CollectionService {

    @Autowired
    private CollectionMapper collectionMapper;

    @Override
    public Integer add(CollectionDTO collectionDTO) {
        DefaultMapperFactory mapperFactory = CommonUtil.getMapperFactory();
        CollectionEntity collectionEntity = mapperFactory.getMapperFacade().map(collectionDTO, CollectionEntity.class);
        String time = CommonUtil.getTime();
        collectionEntity.setCollectionCreateTime(time);
        return collectionMapper.addCollection(collectionEntity);
    }

    @Override
    public Integer delete(CollectionDTO collectionDTO) {
        DefaultMapperFactory mapperFactory = CommonUtil.getMapperFactory();
        CollectionEntity collectionEntity = mapperFactory.getMapperFacade().map(collectionDTO, CollectionEntity.class);
        return collectionMapper.updateCollection(collectionEntity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public TableRspDTO showTable(TableReqDTO tableReqDTO, UserInputDTO userInputDTO) throws Exception {
        DefaultMapperFactory mapperFactory = CommonUtil.getMapperFactory();
        UserEntity prop = mapperFactory.getMapperFacade().map(userInputDTO, UserEntity.class);
        List<CollectionDTO> list = collectionMapper.selectTable(tableReqDTO.getQueryText(), tableReqDTO.getStart(), tableReqDTO.getPageSize(), prop);
        Integer count = collectionMapper.selectCount(tableReqDTO.getQueryText(),prop);
        TableRspDTO res = new TableRspDTO();
        res.setListTable(list);
        res.setTotal(count);
        return res;
    }

    @Override
    public List<CollectionDTO> showUnpassedCollection(UserInputDTO userInputDTO) throws Exception {
        DefaultMapperFactory mapperFactory = CommonUtil.getMapperFactory();
        UserEntity prop = mapperFactory.getMapperFacade().map(userInputDTO, UserEntity.class);
        List<CollectionDTO> collectionDTOList = collectionMapper.selectUnpassedCollection(prop);
        return collectionDTOList;
    }

    @Override
    public CollectionDTO showCollectionInformation(CollectionDTO collectionDTO) throws Exception {
        DefaultMapperFactory mapperFactory = CommonUtil.getMapperFactory();
        CollectionEntity collectionEntity = mapperFactory.getMapperFacade().map(collectionDTO, CollectionEntity.class);
        CollectionDTO res = collectionMapper.selectCollectionById(collectionEntity);
        return res;
    }
}